/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package database;

import com.mongodb.DB;
import com.mongodb.Mongo;
import com.mongodb.MongoClient;
import java.util.HashMap;
import java.util.logging.Level;
import java.util.logging.Logger;
import object.MTObject;
import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SchedulerFactory;
import org.quartz.Trigger;
import org.quartz.TriggerBuilder;
import org.quartz.impl.StdSchedulerFactory;
import utility.MTConstant;

/**
 *
 * @author daolam
 */


public class MTDatabase {
    
    // <editor-fold desc="Singleton">
    private static MTDatabase _instance;
    private MTDatabase()
    {
        initialize();
    }
    
    public static MTDatabase getInstance()
    {
        if (_instance == null)
            _instance = new MTDatabase();
        return _instance;
    }
    //</editor-fold>
    
    //<editor-fold desc="Property">
    private Mongo _mongoDB;
    private DB _mtDB;
    private Scheduler _schedulerLoadData;
    private Scheduler _schedulerSaveData;
    //</editor-fold>
    
    //<editor-fold desc="Get Method">
    //</editor-fold>
     
    //<editor-fold desc="Behavior">
    private void initialize()
    {
        try{
            _mongoDB = new MongoClient(MTConstant.DBServerAddress,
                                MTConstant.DBServerPort);
           
            _mtDB = _mongoDB.getDB(MTConstant.DatabaseName);
            boolean auth = _mtDB.authenticate(MTConstant.UserDatabase,
                                        MTConstant.PasswordDatabase);         
            if (!auth)
            {
                _mtDB = null;
                _mongoDB.close();
            }
            
            SchedulerFactory _schedulerFactory = new StdSchedulerFactory("scheduler/MTLoadData.properties");
            _schedulerLoadData = _schedulerFactory.getScheduler(MTConstant.SchedulerLoadData);
            _schedulerLoadData.start();
            
            _schedulerFactory = new StdSchedulerFactory("scheduler/MTSaveData.properties");
            _schedulerSaveData = _schedulerFactory.getScheduler(MTConstant.SchedulerSaveData);
            _schedulerSaveData.start();
        }catch(Exception e)
        {
            Logger.getLogger("MTDatabase").log(Level.SEVERE, e.getMessage());
        }
        
        
    }
    
    public void saveData(String collectionName, HashMap<Short,String> data) throws SchedulerException
    {
        JobDetail saveJob = JobBuilder.newJob(MTJobSaveData.class).build();
        saveJob.getJobDataMap().put("DBHANDLER",_mtDB);
        saveJob.getJobDataMap().put("COLLECTIONNAME", collectionName);
        saveJob.getJobDataMap().put("DATA", data);
        Trigger trigger = TriggerBuilder.newTrigger().startNow().build();
        _schedulerSaveData.scheduleJob(saveJob, trigger);
    }
    
    public void loadData(String collectionName,long guid) throws SchedulerException
    {
        JobDetail loadJob = JobBuilder.newJob(MTJobLoadData.class).build();
        loadJob.getJobDataMap().put("DBHANDLER",_mtDB);
        loadJob.getJobDataMap().put("COLLECTIONNAME", collectionName);
        loadJob.getJobDataMap().put("GUID",guid);
        Trigger trigger = TriggerBuilder.newTrigger().startNow().build();
        _schedulerLoadData.scheduleJob(loadJob,trigger);
        _schedulerLoadData.getListenerManager().addJobListener(new MTJobLoadDataListener());
    }
    //</editor-fold>
}
